import Vue from 'vue'
import App from './App'
import uView from "uview-ui";
Vue.use(uView);
import msgUtils from './utils/msgUtils.js'
Vue.prototype.$api = msgUtils

import share from './utils/share.js'
Vue.mixin(share)
//luch-request
import Request from 'utils/luch-request/index.js' // 下载的插件
global.$http = new Request();
global.$http.interceptors.request.use((config) => { // 可使用async await 做异步操作
	config.header = {
		...config.header,
		a: 0, // 演示拦截器header加参
		Authorization: 'Bearer ' + uni.getStorageSync('token'), // 演示拦截器header加参
	}
	return config
}, config => { // 可使用async await 做异步操作
	return Promise.reject(config)
})
global.$http.interceptors.response.use((response) => {
	/* 对响应成功做点什么 可使用async await 做异步操作*/
	// console.log('请求成功', response)
	if (response.config.url == '/api/family/await') {
		return response
	}
	if (response.data.code == 1) {
		return response
	}
	if (response.data.code == -1) {
		uni.showToast({
			title: response.data.msg,
			icon: "none",
			mask: true,
			duration: 2000
		})
		uni.clearStorageSync();
		setTimeout(function() {
			uni.reLaunch({
				url: '/pages/logins/login/login'
			})
		}, 1000)
		return
	}
	uni.showToast({
		title: response.data.msg,
		icon: "none",
		mask: true,
		duration: 2000
	})
}, (response) => {
	console.log('请求失败', response)
	/*  对响应错误做点什么 （statusCode !== 200）*/
	if (response.header.a == 0) {
		uni.showToast({
			title: "接口访问失败",
			icon: "none",
			mask: true,
			duration: 2000
		})
		return Promise.reject(response)
	}
})


Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()